<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <div ref="dialogInfoBody">
      <!-- 条件栏 -->
      <page-filter
        :query="filterInfo.query.queryMap"
        :query-more-json="true"
        :filter-list="filterInfo.searchList"
        :filter-btn="filterInfo.searchBtn"
      />
      <DialogTable
        ref="resourceTable"
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :highlight-row="highlightRow"
        :checked-list="checkedList"
        :reserve-selection="true"
        :check-box="checkBox"
        @handleEvent="handleEvent"
      >
      </DialogTable>
    </div>
  </page-dialog>
</template>
<script setup name="SelectProductSingle">
import PageDialog from "../PageDialog/index";
import DialogTable from "../DialogTable/index";
import PageFilter from "../NewPageFilter/index";
import { getCustomizeSetting } from "@/api/customize";
import { getProductCustomizeList } from "@/api/core";

const { proxy } = getCurrentInstance();
const emit = defineEmits();
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getProductCustomizeList,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  showPrice: {
    type: Boolean,
  },
  showSupplierName: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  queryParams: {
    type: Object,
    default: function () {
      return {};
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
  sortString: {
    type: String,
    default: "commonName,standard ",
  },
  tableKey: {
    type: String,
    default: undefined,
  },
  checkedList: {
    type: Array,
    default: () => {
      return [];
    },
  },
});

// 过滤相关配置
const filterInfo = reactive({
  query: {
    page: true,
    customizeUserVO: {},
    sortString: props.sortString,
    queryMap: {
      type: {
        asTbName: "core_product_new",
        columnName: "type",
        queryResource: 0,
        queryType: "EQUALS",
        queryCategory: "AND",
        value: 0,filterInfo
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [{ type: "button", label: "搜索", event: search }],
});
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
});
// 弹窗相关
const dialogInfo = reactive({
  title: "选择产品",
  width: "70%",
  btLoading: false,
  btList: [
    { label: "关闭", event: handleClose, show: true },
    {
      label: "保存",
      type: "primary",
      disabled: false,
      event: save,
      show: true,
    },
  ],
});
const state = reactive({
  selectRow: [],
});

const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emit("update:showDialog", newValue); // 更新父组件shows
  },
  
});

watch(
  () => tableInfo.data,
  (val) => {
    if (val.length && props.checkedList.length) {
      props.checkedList.forEach((row) =>
      proxy.$refs["resourceTable"].toggleRowSelection(row, true)
      );
    }
  }
);

let obj = {
  price: {
    label: "单价",
    value: "price",
    hidden: true,
    type: "column",
    show: false,
    permission: !props.showPrice,
    formatter: proxy.$fn.TripartiteMethod,
  },
  defaultSupplierPrice: {
    label: "默认供应商价格",
    value: "defaultSupplierPrice",
    type: "column",
    permission: !props.showPrice,
    show: false,
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
  },
  showPrice: {
    label: "展示价格",
    value: "showPrice",
    type: "column",
    show: false,
    permission: !props.showPrice,
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
  },
  commonName: {
    label: "通用名称",
    value: "commonName",
    componentType: "input",
    show: false,
    hidden: true,
  },
};
tableInfo.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
});
filterInfo.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
});
getCustomize();

// 获取列表
function getList () {
  tableInfo.refresh = Math.random();
};
function search () {
  tableInfo.initCurpage = Math.random();
  tableInfo.refresh = Math.random();
};
function save () {
  handleClose();
  emit("callback", state.selectRow);
};
function handleEvent (event, data) {
  switch (event) {
    case "tableClickRow":
      state.selectRow = [...data];
      break;
    case "tableCheck":
      state.selectRow = [...data];
      break;
  }
};
// 关闭弹窗前的回调
function handleClose () {
  shows.value = false;
  emit("reset");
};
function getCustomize () {
  
  
  let param = { tableKey: props.tableKey };
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data;
      tableInfo.fieldList = proxy.$fn.setFieldList(
        tableInfo.fieldList,
        data.detail
      );
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.searchList,
        data.detail
      );
      filterInfo.searchList = searchInfo.searchList;
      filterInfo.searchList.forEach((item) => {
        item.event = search;
      });
      filterInfo.query.customizeUserVO = data;
      filterInfo.query.queryMap = {
        ...searchInfo.queryMap,
        ...props.query,
      };
      filterInfo.query.queryParams = props.queryParams;
      tableInfo.api = props.api;
      getList();
    }
  });
};
</script>

<style scoped lang="scss">
.page-dialog {
  .el-dialog__body {
    height: 300px;
    overflow-y: auto;
  }
}
</style>
